﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;

namespace CrabSS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public static string GetHttpResponse(string url, int Timeout)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = Timeout;

             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        public MainWindow()
        {
            InitializeComponent();
            string version = GetHttpResponse("https://v6.crabapi.cn/api/crabss/version?channel=beta",1000);
            string hikotoko = GetHttpResponse("https://v1.hitokoto.cn/?encode=text",1600);
            hitokoto.Content = hikotoko;
            version_online.Content = "联网版本号["+version+"]";
            content.Content = GetHttpResponse("https://v6.crabapi.cn/api/crabss/broadcast?channel=beta&encode=text",1600);
            min.Value = 128;
            max.Minimum = (double)min.Value;
            loading.Visibility = Visibility.Hidden;
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            你被骗了 rickroll = new();
            rickroll.Show();
        }

        private void choosejvav_Click(object sender, RoutedEventArgs e)
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.Filter = "应用程序|*.exe";
            if (fileDialog.ShowDialog() == true)
            {
                jvav.Text = fileDialog.FileName;
            }
        }

        private async void start_Click(object sender, RoutedEventArgs e)
        {
            if (min.Value != null && max.Value != null)
            {
                    loading.Visibility = Visibility.Visible;
                    data.Content = "正在等待内核导入";
                    AddCore core = new();
                    core.Show();
            }
            else
            {
               await this.ShowMessageAsync(":( 出错了，但不是程序的错", "宁的内存无了捏（");
            }
        }
        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            CancellationToken token = new();
            TaskScheduler uiSched = TaskScheduler.FromCurrentSynchronizationContext();
            Task.Factory.StartNew(DialogsBeforeExit, token, TaskCreationOptions.None, uiSched);
        }
        private async void DialogsBeforeExit()
        {
            MessageDialogResult result = await this.ShowMessageAsync(this.Title, "确定退出 CrabSS 吗？", MessageDialogStyle.AffirmativeAndNegative);
            if (result == MessageDialogResult.Negative)
            {
                return;
            }
            else//确认退出
            {
               this.Close();
            }
        }
    }
}
